package com.yjck.fm.selfinfo.action;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class FileUpAction extends ActionSupport {
	private static final long serialVersionUID = 1L;
	private String caption;		//form中其他的上传的字段
	private String fileName;	//上传的文件名
	private String resultMsg = "ok" ;  //上传失败后，返回的错误结果
	private String savePath;
	private String success = "ok";	//json中返回的结果
	private String tip;
	private File upload;		//上传的文件，在extjs 中对应 xtype:'fileuploadfield',  name:'upload'
	private String uploadContentType; //限制上传的后缀名
	private InputStream resultStream;
	
	public String execute() throws Exception{
	 try { //此处为最简单的文件拷贝方法
		String saveName =(String) ServletActionContext.getRequest().getSession().getAttribute("email"); 
		String[] temp = this.fileName.split("\\.");
		String extendType = temp[1];
		String uploadPath = ServletActionContext.getServletContext().getRealPath(savePath)+"\\" ;
		java.io.InputStream is = new FileInputStream(upload);
		java.io.OutputStream os = new FileOutputStream(uploadPath+saveName+"."+extendType);
		byte buffer[] = new byte[8192];
		int count = 0;
		while ((count = is.read(buffer)) > 0)
			os.write(buffer, 0, count);
		os.close();
		is.close();
	 } catch (Exception e)
	 {
		 resultMsg = "上传文件失败，文件太大或没有选择上传的文件 ";
		 success = "faile";
	 }
	 Map<String,String> map = new HashMap<String,String>();
	 map.put("msg", resultMsg);
	 map.put("flag", success);
	// ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
     JSONObject jo=JSONObject.fromObject(map);  
     tip = jo.toString();
	 //此处会根据 success 和 resultMsg 返回 json
	 //{"resultMsg":"ok","success":true}
     //ServletActionContext.getResponse().getWriter().write(tip);
     resultStream =new ByteArrayInputStream(tip.getBytes());
	return "upload";
		
	}
	public String getCaption() {
		return caption;
	}
	public String getResultMsg() {
		return resultMsg;
	}
	public String getSavePath() {
		return savePath;
	}
	
	public String getSuccess() {
		return success;
	}
	public String getTip() {
		return tip;
	}
	public File getUpload() {
		return upload;
	}
	public  String getUploadContentType() {
		return uploadContentType;
	}
	public String getUploadFileName() {
		return fileName;
	}
	public void setCaption(String caption) {
		System.out.println("接收到的caption：" + caption );
		this.caption = caption;
	}
	
	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}
	public void setTip(String tip) {
		this.tip = tip;
	}
	public void setUpload(File upload) {
		this.upload = upload;
		System.out.println("上传到tomcat中的临时文件名：" + upload.getPath());
	}
	public  void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}
	public void setUploadFileName(String fileName) throws UnsupportedEncodingException {
		System.out.println("上传的文件名：" +fileName);
		this.fileName = fileName;
	}
	public InputStream getResultStream() {
		return resultStream;
	}
}
